﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data;
using Timesheet.Service;
using Timesheet.Domain;
using DevExpress.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace Timesheet.WebForm.Projects
{
    public partial class Default:System.Web.UI.Page
    {
        public IProjectService _ProjectService { get; set; } 
        public ILocationsService _LocationService { get; set; } 
        public IDisciplinesService _DisciplineService { get; set; } 
        protected void Page_Load(object sender,EventArgs e)
        {
            if(!IsPostBack)
            {
                //grvProjects.Visible = false;
                btnNew.Visible = false;
                User user = Session["User"] as User;
                if (user == null)
                    Response.Redirect("~/Account/Login.aspx");

                var col = grvProjects.Columns["Action"] as GridViewCommandColumn;

                if (user.Coordinator || user.Manager)
                {

                }
                else 
                {
                    btnNew.Visible = false;
                    grvProjects.Visible = false;
                    ASPxLabel2.Text = "You dont not have permission to access functions";
                }
                if (user.Manager)
                {
                    btnNew.Visible = true;
                    col.Visible = true;
                }
                else
                {
                    col.Visible = false;

                }
            }
        }      
      
        protected void dsPrjects_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _ProjectService; 
        }
        protected void dsLocation_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _LocationService;
        }

         protected void dsDiscipline_ObjectCreating(object sender,ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _DisciplineService;
        }

         protected void Button1_Click(object sender,EventArgs e)
         {
             grvProjects.AddNewRow();
         }        
         protected void grvProjects_RowInserting(object sender,DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
         {
             e.NewValues["Level"] = Session["userrole"];
             e.NewValues["CreateBy"] = Session["UserId"];
         }

         protected void grvProjects_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
         {
             if (e.VisibleIndex == -1) return;

             switch (e.ButtonType)
             {
                 case ColumnCommandButtonType.Edit:
                     e.Enabled = EditButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex);
                     break;
                 case ColumnCommandButtonType.Delete:
                     e.Enabled = EditButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex);
                     break;
             }
         }

         private bool EditButtonVisibleCriteria(ASPxGridView grid, int visibleIndex)
         {
             User user = Session["User"] as User;
             if (user == null)
                 return false;
             object row = grid.GetRow(visibleIndex);
             Timesheet.Domain.Project project = (Timesheet.Domain.Project)row;
             if (user.Manager == true)
             {
                 return true;
             }

             if (project.CreateBy == user.Id)
             {
                     return true;
             }
             return false;

         }
    }
}